﻿--[[
   @brief    gift_op_panel
   @author   harleyxwang
   @version  1.0
   @date     2016-12-22
--]]

ierd_tgp.tpf_ui.Require("frame_bottom\\gift_op_icon.lua");

gift_op_panel = 
{
    MAX_ITEMS = 4;
   
    ctrlId2Item = {};

    gift_cfg_tbl = {};   -- 礼包进度配置表
    stop_glint_percent_tbl = {};        -- 停止闪烁的进度表 

    is_first_init = true;

    cur_game_id = 0;

    parent_id = 0;

    cur_gift_panel_setting_progress = 0;    -- 当前设置的gift栏进度值
    
    show_gift_icon = function (parent_id, game_id)
        if game_id == gift_op_panel.cur_game_id then
            return;
        end
        local panel_ptr = ui_utility.get_ctrl_ptr_by_id(parent_id);
        if panel_ptr == nil then return end

        gift_op_panel.parent_id = parent_id;
        gift_op_panel.cur_game_id = game_id;

        gift_op_panel.init_gift_icons(game_id);
    end;

    hide_gift_icon = function()
        gift_op_panel.cur_game_id = 0;
        -- 删除控件
        for ctrl_id, item in pairs(gift_op_panel.ctrlId2Item) do
            ui_utility.setproperty_by_id(ctrl_id, "Visible", "false");
            layout_mgr.delay_remove_ctrl(ctrl_id);
        end;

        gift_op_panel.ctrlId2Item = {};
    end;

    set_downloading_progress = function(game_id, fProgerss)
        if game_id ~= gift_op_panel.cur_game_id then
            return
        end
        gift_op_panel.cur_gift_panel_setting_progress = fProgerss;
        local stop_glint_percent = 0;
        if gift_op_panel.stop_glint_percent_tbl[game_id] ~= nil then
            stop_glint_percent = gift_op_panel.stop_glint_percent_tbl[game_id];
        end

        for key, icon_item in pairs(gift_op_panel.ctrlId2Item) do
            if nil ~= icon_item then
                if icon_item.percent_ < fProgerss then
                    if icon_item.percent_ < stop_glint_percent then
                        icon_item:set_icon_state(GiftOpIcon.icon_state_active_no_animation);
                    else
                        icon_item:set_icon_state(GiftOpIcon.icon_state_active_animation);
                    end
                else
                    icon_item:set_icon_state(GiftOpIcon.icon_state_no_active);
                end
            end
        end
    end;

    -- 获取下载礼包的进度配置值
    get_gift_progress_cfg = function(game_id)
        local cfg_tbl = {};
        local  module_info = banner_model.get_banner_module_info_by_game_id(game_id);
        if module_info ~= nil and module_info.download_gifts ~= nil and module_info.download_gifts ~= "" then
            local gifts_tbl = base_utility.json_to_table(module_info.download_gifts);
            if gifts_tbl ~= nil and type(gifts_tbl) == 'table' then
                for key, value in pairs(gifts_tbl) do
                    table.insert(cfg_tbl, tonumber(key));
                end
            end
        end
        return cfg_tbl;
    end;

    init_gift_icons = function (game_id)
        gift_op_panel.gift_cfg_tbl = gift_op_panel.get_gift_progress_cfg(game_id);
        -- 删除旧的控件
        for ctrl_id, itme in pairs(gift_op_panel.ctrlId2Item) do
            layout_mgr.delay_remove_ctrl(ctrl_id);
        end;

        gift_op_panel.ctrlId2Item = {};
        -- 获取按钮的背景图片
        local icon_diable_url, icon_url = "";
        local  module_info = banner_model.get_banner_module_info_by_game_id(game_id);
        if module_info ~= nil and module_info.gift_icon_disable ~= nil and module_info.gift_icon_disable ~= "" then
            icon_diable_url = module_info.gift_icon_disable;
        end
        if module_info ~= nil and module_info.gift_icon ~= nil and module_info.gift_icon ~= "" then
            icon_url = module_info.gift_icon;
        end

        for index, percent in pairs(gift_op_panel.gift_cfg_tbl) do
            local item_ctrl_id = layout_mgr.restore_sub_layout_multi(gift_op_panel.parent_id, "GiftIcon.tpf");
            local item = GiftOpIcon:new(percent, item_ctrl_id, game_id, index);
            if nil ~= item then
                gift_op_panel.ctrlId2Item[item_ctrl_id] = item;
                item:init_icon_url(icon_diable_url, icon_url);  
            end 
        end
        -- 进行异步布局，解决父控件还没有布局完成的情况
        local param = {};
        param["game_id"] = game_id;
        local json = base_utility.table_to_json(param);
        event_center.post_event(frame_bottom_event.GIFT_ICON_LAYOUT_EVENT, json, true);
    end;

    do_layout = function()
        local panel_ptr = ui_utility.get_ctrl_ptr_by_path(gift_op_panel.parent_id, "LvlProgressBarBackground");
        if nil == panel_ptr then
            return;
        end
        
        -- 根据进度条的宽度来计算每个icon的偏移值
        local width = 0;
        local panel_rct = panel_ptr:GetRectEx(FALSE,TRUE);
        if panel_rct ~= nil then
            width = panel_rct.right - panel_rct.left;
        end
        local offset_table = {};
        if width == 0 then return end
        for index, percent in ipairs(gift_op_panel.gift_cfg_tbl) do
            table.insert(offset_table, math.floor(width * percent / 100));
        end

        for key, val in pairs(gift_op_panel.ctrlId2Item) do
            if nil ~= val then
                local wrapper_id = val:get_wrapper_id();
                local icon_ptr = ui_utility.get_ctrl_ptr_by_id(wrapper_id);
                if nil ~= icon_ptr then
                    local offset_x = offset_table[val.index_];
                    local rct = icon_ptr:GetRectEx(FALSE,TRUE);
                    if nil ~= offset_x and nil ~= rct then
                        local width = rct.right - rct.left;
                        local height = rct.bottom - rct.top;
                        rct.left = panel_rct.left + offset_x - 10;
                        rct.right = rct.left + width;
                        rct.top = panel_rct.top - 11;
                        rct.bottom = rct.top + height;
                        icon_ptr:MoveEx(rct,TRUE);
                    end
                end
            end
        end
    end;

    get_gift_icon_item = function (item_ctrl_id)
        local item = nil;
        for ctrl_id, val in pairs(gift_op_panel.ctrlId2Item) do
            if item_ctrl_id == ctrl_id then
                item = val;
                break;
            end
        end
        return item;
    end;

    on_gift_icon_hover = function(game_id, percent, icon_offset)
        local callback_func = web_register_center.get_func_by_browser(
            "hover_gift_icon_event", CID_MAINFRAME_QBWEB);
        if callback_func ~= nil and callback_func ~= "" then
            local rsp_tbl = web_utility.notify_event_comm_data("hover_gift_icon_event");
            rsp_tbl.data.cur_percent = gift_op_panel.cur_gift_panel_setting_progress;
            rsp_tbl.data.percent = percent;
            rsp_tbl.data.game_id = game_id;
            rsp_tbl.data.position = icon_offset;
            web_utility.lua_call_js_ex(callback_func, 
                    base_utility.table_to_json(rsp_tbl), CID_MAINFRAME_QBWEB);
        end
    end;

    on_gift_icon_leave = function()

    end;

    handle_mainframe_posithon_change = function(game_id)
        if game_id ~= gift_op_panel.cur_game_id then
            return
        end
        gift_op_panel.do_layout();
    end;

    handle_gift_icon_layout = function(event_id, json_str)
        local param = base_utility.json_to_table(json_str);
        if param == nil then
            base_utility.log("[gift_op_panel] bad json param.");
            return;
        end
        if param.game_id ~= gift_op_panel.cur_game_id then
            return;
        end
        gift_op_panel.do_layout();
    end;

    stop_animation = function(json_str)
        local param_table = base_utility.json_to_table(json_str);
        local game_id = nil;
        if param_table ~= nil and param_table.data ~= nil and param_table.data.game_id ~= nil then
             game_id = tonumber(param_table.data.game_id);
        end
        local rsp_table = web_utility.web_rsp_comm_data(param_table, 0, "");
        rsp_table.data = {game_id = game_id};   
        if param_table.callback_func ~= nil and param_table.callback_func ~= "" then
            local rsp_json = base_utility.table_to_json(rsp_table);
            web_utility.lua_call_js_ex(param_table.callback_func, rsp_json, CID_MAINFRAME_QBWEB);
        end
        if game_id == nil or game_id == 0 then return end
        if gift_op_panel.cur_game_id ~= game_id then
            return
        end
        -- 获取当前的进度：
        local game_state = banner_model.get_game_state(game_id);
        if game_state ~= nil then
            local progress = game_state.iProgress;
            gift_op_panel.stop_glint_percent_tbl[game_id] = progress;

            for key, icon_item in pairs(gift_op_panel.ctrlId2Item) do
                if nil ~= icon_item then
                   if icon_item.percent_ < progress then
                       icon_item:set_icon_state(GiftOpIcon.icon_state_active_no_animation);
                   end
                end
            end
        end
    end;
};
